{
    "cells": [
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "# Sparse imaging with LASSO\n",
                "\n",
                "This example generates a sparse signal and tries to recover it using lasso\n"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "from __future__ import print_function\n",
                "from __future__ import division\n",
                "from sklearn import linear_model\n",
                "import matplotlib.pyplot as plt\n",
                "import numpy as np\n",
                "from scipy import misc\n",
                "from IPython import display\n",
                "from simulator import *\n",
                "%matplotlib inline\n"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "We generate an orthogonal matrix X and compute measurements = Xw+z where w is the vectorized format of the sparse image\n"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "measurements, X, I = simulate()\n",
                "\n",
                "# THE SETTINGS FOR THE IMAGE - PLEASE DO NOT CHANGE\n",
                "height = 91\n",
                "width = 120\n",
                "imDims = (height, width)\n",
                "sparsity = 476\n",
                "numPixels = len(X[0])\n",
                "\n",
                "plt.imshow(I, cmap=plt.cm.gray, interpolation='nearest');\n",
                "plt.title('Original Sparse Image')\n"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "We plot matrix X:\n"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "chosenMaskToDisplay = 0\n",
                "M0 = X[chosenMaskToDisplay].reshape((height, width))\n",
                "plt.title('Matrix X')\n",
                "plt.imshow(M0, cmap=plt.cm.gray, interpolation='nearest');\n"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "And here is the plot of measurement vector:\n"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "# measurements\n",
                "plt.title('measurement vector (y)')\n",
                "plt.plot(measurements)\n",
                "plt.xlabel('measurement index')\n",
                "plt.show()\n"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "We use lasso to recover the image:\n"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": [
                "def LASSO(imDims, measurements, X, _lambda):\n",
                "    clf = linear_model.Lasso(alpha=_lambda)\n",
                "    clf.fit(X, measurements)\n",
                "    Ihat = clf.coef_.reshape(imDims)\n",
                "    plt.title('estimated image')\n",
                "    plt.imshow(Ihat, cmap=plt.cm.gray, interpolation='nearest')\n",
                "    return clf.coef_\n"
            ]
        },
        {
            "cell_type": "markdown",
            "metadata": {},
            "source": [
                "Change the lasso regularization parameter to recover the image and report the value.\n"
            ]
        },
        {
            "cell_type": "code",
            "execution_count": null,
            "metadata": {},
            "outputs": [],
            "source": []
        }
    ],
    "metadata": {
        "kernelspec": {
            "display_name": "Python 3",
            "language": "python",
            "name": "python3"
        },
        "language_info": {
            "codemirror_mode": {
                "name": "ipython",
                "version": 3
            },
            "file_extension": ".py",
            "mimetype": "text/x-python",
            "name": "python",
            "nbconvert_exporter": "python",
            "pygments_lexer": "ipython3",
            "version": "3.8.3"
        }
    },
    "nbformat": 4,
    "nbformat_minor": 2
}